package sit.engreal.leetcode.problems300;

import org.junit.jupiter.api.Assertions;

import java.util.HashMap;
import java.util.HashSet;

/**
 * <a href="https://leetcode.cn/problems/contains-duplicate/">217. 存在重复元素</a>
 */
public class LeetCode217 {
    public boolean containsDuplicate(int[] nums) {
        final HashMap<Integer, Integer> map = new HashMap<>();
        final HashSet<Integer> set = new HashSet<>();
        for (int n : nums) {
            final boolean add = set.add(n);
            if (!add) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        final LeetCode217 demo = new LeetCode217();
        {
            Assertions.assertTrue(
                    demo.containsDuplicate(new int[]{1, 2, 3, 1})
            );
        }
        {
            Assertions.assertFalse(
                    demo.containsDuplicate(new int[]{1,2,3,4})
            );
        }
        {
            Assertions.assertTrue(
                    demo.containsDuplicate(new int[]{1,1,1,3,3,4,3,2,4,2})
            );
        }
    }
}
